/**
 * Copyright (c) 时间, Tal.Ben 袁东昊 (ydonghao2@gmail.com).
 *
 * 版权
 */
package com.talben.soudian.fragment.viewpagerfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.talben.soudian.R;
import com.talben.soudian.adapter.ViewPageFragmentAdapter;
import com.talben.soudian.base.BaseListFragment;
import com.talben.soudian.base.BaseViewPagerFragment;
import com.talben.soudian.bean.ApkList;
import com.talben.soudian.fragment.ApkInstalledFragment;
import com.talben.soudian.fragment.GiftFragment;
import com.talben.soudian.fragment.GiftGotFragment;
import com.talben.soudian.fragment.NewApkFragment;
import com.talben.soudian.ui.OnTabReselectListener;


/**
 * @Title: ApkViewPagerFragment.java
 * @Package com.talben.soudian.fragment.viewpagerfragment
 * @Description: TODO(添加描述)
 * @author Tal.Ben
 * @date 2015年5月25日 下午10:05:01
 * @version v1.0
 */
public class HomeViewPagerFragment extends BaseViewPagerFragment implements
 	OnTabReselectListener {

	@Override
    protected void setScreenPageLimit() {
        mViewPager.setOffscreenPageLimit(3);
    }
	
	@Override
	public void onTabReselect() {
		try {
            int currentIndex = mViewPager.getCurrentItem();
            Fragment currentFragment = getChildFragmentManager().getFragments()
                    .get(currentIndex);
            if (currentFragment != null
                    && currentFragment instanceof OnTabReselectListener) {
                OnTabReselectListener listener = (OnTabReselectListener) currentFragment;
                listener.onTabReselect();
            }
        } catch (NullPointerException e) {
        }
	}
	
	@Override
	protected void onSetupTabAdapter(ViewPageFragmentAdapter adapter) {
		String[] title = getResources().getStringArray(
                R.array.apps_viewpage_arrays);
        adapter.addTab(title[0], "apps", NewApkFragment.class,
        		getBundle(ApkList.CATALOG_ALL));
        adapter.addTab(title[1], "update", ApkInstalledFragment.class,
        		getBundle(2));
        adapter.addTab(title[2], "gift", GiftFragment.class,
        		getBundle(3));
        adapter.addTab(title[3], "giftlog", GiftGotFragment.class,
        		getBundle(1));
	}

	private Bundle getBundle(int newType) {
        Bundle bundle = new Bundle();
        bundle.putInt(BaseListFragment.BUNDLE_KEY_CATALOG, newType);
        return bundle;
    }
}

